import java.util.Scanner;

/**
 * @author gao
 * @date 2025/11/13 21:28
 */

public class Solution {
    public int iceBreakingGame(int num, int target) {
        return josephus(num, target);
    }

    private int josephus(int num, int k) {
        // 只有1人时，最后剩下的人的编号为0
        if (num == 1) {
            return 0;
        }

        // 递归计算 n-1，然后映射 n 个人
        return (josephus(num - 1, k) + k) % num;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int num = in.nextInt();
            int target = in.nextInt();

            Solution solution = new Solution();
            System.out.println(solution.iceBreakingGame(num, target));
        }
    }
}